package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.IClassLectureService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.educational.service.IStudentService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 处理已经支付&超时撤销的订单
 * Created by Administrator on 2018/4/9.
 */
@JobHander(value = "CorrectCourseOrderStatusTask")
@Component
public class CorrectCourseOrderStatusTask extends IJobHandler {

    @Resource
    private IClassLectureService classLectureService;

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private IStudentService studentService;


    @Override
    public ReturnT<String> execute(String... params) throws Exception {

        Example<StudentReg> example = StudentReg.createExample();
        example.createCriteria().andEqualTo("regStatus", 4).andEqualTo("chargingStatus", 2);

        List<StudentReg> list = studentRegService.findListByExample(example);

        for (StudentReg studentReg : list) {
            StudentReg u = new StudentReg();
            u.setRegId(studentReg.getRegId());
            u.setRegStatus(0);
            studentRegService.updateSelectiveById(u);
            studentReg.setRegStatus(0);
            classLectureService.createAttendanceRecordForStudent(Arrays.asList(studentReg));
            studentService.updateRegClassAmount(Arrays.asList(studentReg.getStudentId()));
        }

        return ReturnT.SUCCESS;
    }


}
